package com.isecommerce.application.datawizard;

import java.io.PrintWriter;
import java.util.*;

import com.isecommerce.framework.util.Config;
import com.isecommerce.framework.util.LogWriter;
import com.isecommerce.application.datawizard.entity.*;

/**
 * Output CSV File Meta Info Manager<br><br>
 *
 * This class cache the Output CSV file meta info from 'conf.proeprties'.
 * 
 *
 * @version 1.2.5
 * @author Leslie Chen
 */
// History

public class OutputCSVFileInfoManager {
    
    protected Hashtable<String,OutputCSVFileInfoEntity> infoTable;
    protected ArrayList<OutputCSVFileInfoEntity>        infoArray;
    protected String mainClass;
    protected String outputPath;
    protected int csvFileCount;
    
    private LogWriter logger;
            
    /**
     * Initializing manager from application configuration file.
     *
     */
    public void init(Config config){
    	
    	// Init logger
    	this.logger = LogWriter.getLogWriterInstance("DATA_WIZARD", LogWriter.INFO, new PrintWriter(System.out));    
    	this.logger.setOwner(this.getClass().getSimpleName());
    	    	    	
    	this.csvFileCount = Integer.parseInt(config.getValue("output.csvfile.count").trim());
    	this.outputPath = config.getValue("output.path").trim();
        this.mainClass = config.getValue("output.class.main").trim();
    	
    	infoTable = new Hashtable<String,OutputCSVFileInfoEntity>();
        infoArray = new ArrayList<OutputCSVFileInfoEntity>();
                
        OutputCSVFileInfoEntity info = null;
        
        String id = "";
    	
        for(int i=1; i<=this.csvFileCount; i++) {
            id = String.valueOf(i);
            
            try{
            	
            	// Get CSV file info
               	info = new OutputCSVFileInfoEntity(config.getValue("output.csv." + id + ".csvname"),
     		                this.getOutputPath() + config.getValue("output.csv." + id + ".filename"),
                                                   config.getValue("output.csv." + id + ".column.delimiter"),
                                                   config.getValue("output.csv." + id + ".encode"),
                              Boolean.parseBoolean(config.getValue("output.csv." + id + ".enable").trim()));
               	
               	infoTable.put(info.csvName, info);
               	infoArray.add(info);
     
               	this.logger.log("[OK] '" + info.csvName + "' Output CSV File Meta Info registered.", LogWriter.INFO);
               	this.logger.log("[DELIMITER] '" + info.colDel + "'", LogWriter.DEBUG);
               	
            }catch(Exception e){
            	this.logger.log(e, "Properties not found", LogWriter.ERROR);
            }          	                     
            
        }         	
        
        this.logger.log(infoArray.size() + " Output CSV FileInfo registered successfully.", LogWriter.INFO);
    }
    
    /**
     * Get csv file info by given csv alias name.
     *
     * @param name Given csv alias name.
     * @return csv file info entity.
     */
    public OutputCSVFileInfoEntity getCSVfileInfoByKey(String csvname){
        //System.out.println("CSV File Info Name -> " + csvname);
        return infoTable.get(csvname);
    }  
    
    /**
     * Get csv file info by given index.
     *
     * @param index Given index number.
     * @return csv file info entity.
     */
    public OutputCSVFileInfoEntity getCSVfileInfoByIndex(int index){
        //System.out.println("CSV File Info Index -> " + index);
        return infoArray.get(index);
    }
    
    public String getMainClassName(){
    	return this.mainClass;
    }
    
    public String getOutputPath(){
    	return this.outputPath;
    }
    
    public int getFileCount(){
    	return this.csvFileCount;    	
    }
}
